package com.app.oasystem.service.impl;

import com.app.oasystem.dto.ActionResult;
import com.app.oasystem.dto.MailDto;
import com.app.oasystem.entity.Email;
import com.app.oasystem.entity.User;
import com.app.oasystem.mapper.EmailMapper;
import com.app.oasystem.mapper.UserMapper;
import com.app.oasystem.service.EmailService;
import com.app.oasystem.utils.MailsUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86+
 * @data 2021-04-13
 */

@Service
public class EmailServiceImpl extends ServiceImpl<EmailMapper, Email> implements EmailService {

    @Resource
    UserMapper userMapper;

    @Resource
    EmailMapper emailMapper;

    @Override
    public Object sendMail(MailDto mailDto) {
        String toMail = mailDto.getToMail();
        User toUser = userMapper.selectOne(new QueryWrapper<User>().eq("mail", toMail));
        if (null == toUser) {
            return new ActionResult(true, "此系统中不存在该用户!",0);
        }
        String fromMail = mailDto.getFromMail();
        User fromUser = userMapper.selectOne(new QueryWrapper<User>().eq("mail", fromMail));
        Email email = new Email();
        email.setTitle(mailDto.getTitle());
        email.setIsRead(0);
        email.setIsDel(0);
        email.setContent(mailDto.getMailContent());
        email.setFromId(fromUser.getId());
        email.setToId(toUser.getId());
        email.setCreatetime(new Date());
        email.setAttachment("");
        int row = 0;
        try {
            row = emailMapper.insert(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (0 == row){
            return new ActionResult(true, "发送失败!",row);
        }
        return new ActionResult(true, "发送成功!",row);
    }

    @Override
    public List<Email> getEmailList(int condition, Integer userId) {
        List<Email> emailList = new ArrayList<>();
        try {
            emailList = emailMapper.selectList(new QueryWrapper<Email>().eq("fromId", userId).eq("isDel", condition).orderByDesc("createtime"));
            if (null == emailList || emailList.size()==0){
                return emailList;
            }
            List<Integer> list = emailList.stream().map(Email::getToId).collect(Collectors.toList());
            List<String> username = userMapper.selectList(new QueryWrapper<User>().in("id", list)).stream().map(User::getUsername).collect(Collectors.toList());
            for (int i = 0; i < username.size() - 1; i++) {
                Email email = emailList.get(i);
                email.setUsername(username.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return emailList;
    }

    @Override
    public MailDto getEmailInfoById(Integer mailId) {
        Email email = emailMapper.selectOne(new QueryWrapper<Email>().eq("id", mailId));
        MailDto mailDto = new MailDto();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", email.getToId()));
        mailDto.setToMail(user.getMail());
        mailDto.setTitle(email.getTitle());
        mailDto.setMailContent(email.getContent());
        return mailDto;
    }

    @Override
    public boolean deleteEmailById(Integer mailId) {
        int i = emailMapper.deleteById(mailId);
        return i > 0;
    }

    @Override
    public void updateEmailStatus(Integer mailId) {
        Email email = emailMapper.selectOne(new QueryWrapper<Email>().eq("id", mailId));
        email.setIsRead(1);
        emailMapper.updateById(email);
    }

    @Override
    public boolean resetEmailStatus(Integer mailId) {
        Email email = emailMapper.selectOne(new QueryWrapper<Email>().eq("id", mailId));
        email.setIsDel(0);
        int i = emailMapper.updateById(email);
        return i > 0;
    }
}
